package ca.swank.hq.ai;

import ca.swank.hardquor.*;
import ca.swank.hq.optimized.*;

import java.util.LinkedList;
import java.util.TreeSet;
import java.util.Comparator;
import java.util.ArrayList;
import java.util.Iterator;

/**
 * A hardquorBoard that maps the Move class to the low-level hardquorBoard
 * integer moves.
 *
 * @author Jesse Wilson
 */
public class AIBoard extends OptimizedBoard
{
  public AIBoard()
  { super();
  }

  public AIBoard(AIBoard other)
  { super(other);
  }

  public AIBoard(int width, int height, int playerStartWalls)
  { super(width, height, playerStartWalls);
  }

  public void unMove(WalkMove walkMove)
  { unMove(walkMove.source(), walkMove.target());
  }

  public void unMove(WallMove wallMove)
  { unMove(wallMove.a1(), wallMove.a2(), wallMove.b1(), wallMove.b2());
  }

  public void unMove(Move move)
  { if(move instanceof WallMove) unMove((WallMove)move);
    else unMove((WalkMove)move);
  }


  /**
   * generates a string 'image' of the board
   */
  public String toString()
  {
    int width = getWidth();
    int height = getHeight();

    char[][] boardString = new char[height*2+1][width*2+2];

    int[] currentSquare;

    for(int r = height-1; r >= 0; r--)
    { boardString[r*2][0] = '|';
      boardString[r*2+1][0] = ' ';
      boardString[r*2][width*2+1] = '\n';
      boardString[r*2+1][width*2+1] = '\n';
    }

    for(int c = 0; c < width; c++)
    { boardString[height*2][c*2+2] = '+';
      boardString[height*2][c*2+1] = '-';
    }

    for(int r = height-1; r >= 0; r--)
    { for(int c = 0; c < width; c++)
      {
        currentSquare = new int[] { c, height - 1 - r };

        int occupant = getSquareOccupant(currentSquare);
        if(occupant == PLAYER_NONE) boardString[r*2+1][c*2+1] = ' ';
        else if(occupant == PLAYER_ONE) boardString[r*2+1][c*2+1] = '1';
        else if(occupant == PLAYER_TWO) boardString[r*2+1][c*2+1] = '2';

        boolean right = isAdjacent(currentSquare, DIR_RIGHT);
        if(right) boardString[r*2+1][c*2+2] = ' ';
        else boardString[r*2+1][c*2+2] = '|';

        boolean above = isAdjacent(currentSquare, DIR_ABOVE);
        if(above) boardString[r*2][c*2+1] = ' ';
        else boardString[r*2][c*2+1] = '-';

        boolean crisscross = isAdjacent(currentSquare, DIR_CRISSCROSS);
        if(crisscross) boardString[r*2][c*2+2] = ' ';
        else boardString[r*2][c*2+2] = '+';

      }
    }

    StringBuffer result = new StringBuffer();
    for(int r = 0; r < boardString.length; r++)
    { result.append(boardString[r]);
    }

    return result.toString();

  }


}
